import 'dart:convert';

import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/pages/daily_mission_report_module/my_mission_page.dart';
import 'package:app/router/app_pages.dart';
import 'package:app/services/dio_manager.dart';
import 'package:app/utils/hexcolor.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart' as Datee;

// import 'package:image_pickers/image_pickers.dart';

import 'dart:io';

import '../../../common/colors.dart';
import '../../../components/bottom_enter_page.dart';
import '../../../utils/search_module.dart';
import '../../contact_module/contact_active_page.dart';
import '../../contact_module/contact_organ_page.dart';
import '../../personnel_module/personnel_view.dart';
import 'published_calendar_controller.dart';

class PubLishedCalendarView extends GetView {

  PublishedCalendarController controller =
      Get.put(PublishedCalendarController());
  bool submit = false;

  //确认弹窗
  void confirmDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text("温馨提示"),
          content: const Text("您确定要删除当前日程？"),
          actions: <Widget>[
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text("取消"),
            ),
            TextButton(
              child: const Text("确定"),
              onPressed: () {
                controller.requestDataDelete();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      // resizeToAvoidBottomInset: false, // 就是这里
      backgroundColor: AppColor.bgColor,
      appBar: BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: Text(controller.calendar_id > 0 ? '修改日程' : '发布日程'),
      ),
      // AppBar(
      //   backgroundColor: AppColor.themeColor,
      //   leading: IconButton(
      //     onPressed: () {
      //       Get.back();
      //     },
      //     icon: Image.asset(
      //       'images/leftarr.png',
      //       color: Colors.white,
      //       width: 15,
      //       height: 15,
      //     ),
      //   ),
      //   title: Text(controller.calendar_id > 0 ? '修改日程' : '发布日程'),
      //   actions: [
      //     Visibility(
      //         visible: controller.calendar_id > 0,
      //         child: TextButton(
      //             onPressed: () {
      //               confirmDialog(context);
      //             },
      //             child: const Icon(Icons.delete_forever_sharp,
      //                 color: Colors.white, size: 24)))
      //   ],
      // ),
      body: GetBuilder<PublishedCalendarController>(builder: (_) {
        return GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () {
            /// 页面点击响应
            SearchModule.closeKeyboard(context);
          },
          onPanDown: (_) {
            /// 列表滑动响应
            SearchModule.closeKeyboard(context);
          },

          child: Column(
            children: [
              Expanded(
                child: ListView(
                  children: [
                    BrnInputText(
                      maxHeight: 200,
                      minHeight: 30,
                      minLines: 1,
                      maxLength: 50,
                      autoFocus: false,
                      bgColor: Colors.white,
                      textInputAction: TextInputAction.newline,
                      maxHintLines: 20,
                      textEditingController: controller.editingController,
                      hint: '描述一下您的日程名称',
                      padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                      onTextChange: (text) {},
                      onSubmit: (text) {
                        print(text);
                      },
                    ),
                    const SizedBox(
                      height: 1,
                    ),

                    submit==false?SizedBox():controller.editingController.text==''?buildFieldError('请输入日程名称'):SizedBox(),
                    Container(
                      padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                      color: HexColor('#F8F8F8'),
                      width: Get.width - 80,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [Text("基本信息")],
                      ),
                    ),
                    const SizedBox(
                      height: 1,
                    ),
                    GestureDetector(

                        onTap: () async {

                          controller.taoClickUser();

                          // Get.to(PersonnelView());
                          // var data = await Get.to(
                          //     ContactOrganPage(positid: controller.chargpositid));
                          // if (data['data'] == 'refresh') {
                          //   controller.chargename = data['name'];
                          //   controller.chargpositid = data['position'];
                          //   controller.update();
                          // }
                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("组织人"),
                              Container(
                                  height: 40,
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Text(controller.userIdName[0]),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),
                    GestureDetector(
                        onTap: () {
                          if(controller.isAllDaily==false){
                            DatePicker.showDateTimePicker(context,
                                showTitleActions: true,
                                minTime: DateTime(2000, 11, 11),
                                onChanged: (date) {},
                                onConfirm: (date) {
                                  var timeFormat = Datee.DateFormat("yyyy-MM-dd HH:mm");
                                  var timeStr = timeFormat.format(date);
                                  controller.start_time = timeStr;
                                  controller.refresh();
                                },
                                currentTime: DateTime.now(),
                                locale: LocaleType.zh);
                          }else{
                            DatePicker.showDatePicker(context,
                              showTitleActions: true,
                              minTime: DateTime(2000,11,11),
                              onConfirm: (date){
                                var timeFormat = Datee.DateFormat("yyyy-MM-dd");
                                var timeStr = timeFormat.format(date);
                                controller.start_time = timeStr;
                                controller.refresh();
                              },
                              currentTime: DateTime.now(),
                              locale: LocaleType.zh
                            );
                          }
                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("起始日"),
                              Container(
                                  height: 40,
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Text(controller.start_time),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),
                    submit==false?SizedBox():controller.start_time=='请选择开始时间'?buildFieldError('请选择开始时间'):SizedBox(),

                    GestureDetector(
                        onTap: () {
                          if(controller.isAllDaily==false){
                            DatePicker.showDateTimePicker(context,
                                showTitleActions: true,
                                maxTime: DateTime(2030, 12, 12),
                                onChanged: (date) {}, onConfirm: (date) {

                                  var timeFormat = Datee.DateFormat("yyyy-MM-dd HH:mm");
                                  var timeStr = timeFormat.format(date);
                                  controller.ended_time = timeStr;
                                  controller.refresh();
                                },
                                currentTime: DateTime.now(),
                                locale: LocaleType.zh);
                          }else{
                            DatePicker.showDatePicker(context,
                                showTitleActions: true,
                                maxTime: DateTime(2030, 12, 12),
                                onChanged: (date) {}, onConfirm: (date) {

                                  var timeFormat = Datee.DateFormat("yyyy-MM-dd");
                                  var timeStr = timeFormat.format(date);
                                  controller.ended_time = timeStr;
                                  controller.refresh();
                                },
                                currentTime: DateTime.now(),
                                locale: LocaleType.zh
                            );
                          }

                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("到期日"),
                              Container(
                                  height: 40,
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Text(controller.ended_time),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),
                    submit==false?SizedBox():controller.ended_time=='请选择结束时间'?buildFieldError('请选择结束时间'):SizedBox(),

                    GestureDetector(
                        onTap: () {},
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("全天"),
                              Container(
                                  height: 40,
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      CupertinoSwitch(
                                        value: controller.isAllDaily,
                                        onChanged: (bool value) {
                                          controller.isAllDaily =
                                          !controller.isAllDaily;
                                          controller.refresh();
                                        },
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),
                  controller.isAllDaily==true?Container():GestureDetector(
                        onTap: () {},
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 30, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("持续多久（分钟）"),
                              controller.start_time=='请选择开始时间'
                                  ||controller.ended_time=='请选择结束时间'?Container(): Container(
                                  height: 40,
                                  width: 50,
                                  // color: Colors.red,
                                  alignment: Alignment.center,
                                  child: TextField(
                                    enabled: false,
                                    controller: controller.durationTimeTF..text = '${
                                    SearchModule.timeIntervalHour(DateTime.parse(controller.start_time),
                                        DateTime.parse(controller.ended_time))
                                    }',
                                    textDirection: TextDirection.rtl,
                                    decoration: InputDecoration(
                                      isCollapsed: true,
                                      border: InputBorder.none,
                                      hintText: '持续时间'
                                    ),
                                  )
                              ),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 20,
                    ),

                    GestureDetector(
                        onTap: () async {

                          controller.tapClickAlarmType();

                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 25, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("提醒类型"),
                              Container(
                                  height: 40,
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Container(
                                        // color: Colors.yellow,
                                          height: 40,
                                          alignment: Alignment.centerRight,
                                          // width: 250,
                                          child: Text(controller.alarmName.join(','))),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),

                    GestureDetector(
                        onTap: () async {

                          controller.tapClickUserIds();

                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("参与者"),
                              Container(
                                  height: 40,
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Container(
                                          height: 40,
                                          alignment: Alignment.centerRight,
                                          width: 250,
                                          child: Text(controller.userIdsName.join(','),
                                          textAlign: TextAlign.end,
                                          )),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),
                    GestureDetector(
                        onTap: () async {

                          controller.tapClickShareUser();

                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: Colors.white,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("共享人员"),
                              Container(
                                  height: 40,
                                  // color: Colors.red,
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Container(
                                          height: 40,
                                          alignment: Alignment.centerRight,
                                          width: Get.width-120,
                                          child: Text(controller.shareUserIdsName.join(','),
                                            textAlign: TextAlign.end,),

                                      ),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),
                    GestureDetector(
                        onTap: () {
                          controller.tapClickAccessory(['公开的', '私人可见', '部门可见']);
                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("公开程度"),
                              Container(
                                  height: 40,
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Text("${controller.privacyName}"),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),

                    submit==false?SizedBox():controller.privacyName=='选择公开程度'?buildFieldError('选择公开程度'):SizedBox(),

                    GestureDetector(
                        onTap: () {
                          controller.tapClickRoom();
                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("会议室"),
                              Container(
                                  height: 40,
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Text("${controller.roomName}"),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),


                    const SizedBox(
                      height: 1,
                    ),
                    GestureDetector(
                        onTap: () {

                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("位置"),
                              Container(
                                  height: 35,
                                  width: 120,
                                  color: Colors.white,
                                  alignment: Alignment.center,
                                  padding: EdgeInsets.only(left: 5),
                                  child: TextField(
                                    // textDirection: TextDirection.,
                                    controller: controller.locationTF,
                                    textDirection: TextDirection.rtl,
                                    decoration: InputDecoration(
                                      isCollapsed: true,
                                      hintText: '输入位置会议室位置',
                                      border: InputBorder.none
                                    ),
                                  )
                              ),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),
                    BrnInputText(
                      maxHeight: 200,
                      minHeight: 30,
                      minLines: 1,
                      maxLength: 50,
                      autoFocus: false,
                      bgColor: Colors.white,
                      textInputAction: TextInputAction.newline,
                      maxHintLines: 20,
                      textEditingController: controller.editingController1,
                      hint: '描述',
                      padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                      onTextChange: (text) {},
                      onSubmit: (text) {
                        print(text);
                      },
                    ),
                  ],
                ),
              ),
              // Expanded(child: BottomEnterPage()),
              Container(
                color: Colors.white,
                child:  BottomEnterPage(() {
                  submit = true;
                  controller.update();
                  if(controller.editingController.text==''||controller.start_time == '请选择开始时间'
                      ||controller.privacyName == '选择公开程度'){
                    BotToast.showText(text: '请填写完整信息');
                    return;
                  }
                  controller.requestDataWithPublish();

                  print('点击了发布');
                }),
              )
            ],
          ),
        );
      }),
    );
  }
  Widget buildFieldError(errorType) {
    return Padding(padding: EdgeInsets.only(left: 15),child: Row(
      children: [
        // if(errors.contains(errorType))
        Expanded(
          child: Text(
              '${errorType}',
              textAlign: TextAlign.left,
              style: TextStyle(
                  color: Colors.red,
                  fontSize: 12
              )
          ),
        ),
      ],
    ),);
  }

}
